#include "textview.h"
#include <QPainter>
#include <QDir>
#include <QMouseEvent>

TextView::TextView(QWidget *parent)
	: QLabel(parent), mHovered(false), mbContinueMouseEvent(false), mIsTxtChanged(false), mbPassWard(false)
{
	setWindowFlags(Qt::FramelessWindowHint);
}

TextView::~TextView()
{
}

void TextView::setUI(TEXTVIEW_INFO& ui)
{
	mUI = ui;
	if (mUI.rectBmp.width() == 0) {
		mUI.rectBmp = rect();
	}
	if (mUI.rectTxt.width() == 0) {
		mUI.rectTxt = mUI.rectBmp;
	}
	
	if (mUI.backgroundBmp.compare(QString("bgq")) == 0) {
		mPixmap = QPixmap(mUI.rectBmp.size());
		mPixmap.fill(Qt::black);
    } else if (mUI.backgroundBmp.isEmpty()) {
        mPixmap = QPixmap(mUI.rectBmp.size());
        mPixmap.fill(mUI.backgroundColor);
    } else {
        mPixmap = QPixmap(GetBMPPATH(mUI.backgroundBmp));
    }
	mIsTxtChanged = true;
	update();
}

void TextView::mousePressEvent(QMouseEvent * e)
{
	if (mUI.index >= 0) {
		emit send(mUI.index);		
	} else {
		emit clicked();		
	}	
}

void TextView::sethovered(bool hovered) 
{ 
	mHovered = hovered;
	update();
}

void TextView::setBackgroundBmp(const QString& backgroundBmp)
{
	if (mUI.backgroundBmp.compare(backgroundBmp) == 0 && !mPixmap.isNull())
		return;

	mUI.backgroundBmp = backgroundBmp;
	
	if (mUI.backgroundBmp.compare(QString("bgq")) == 0) {
		mPixmap = QPixmap(mUI.rectBmp.size());
		mPixmap.fill(Qt::black);
	} else {
		mPixmap = QPixmap(GetBMPPATH(mUI.backgroundBmp));
	}
	
	update();
}

void TextView::setExternalBmp( QString& bmpFile )
{
	mPixmap = QPixmap(bmpFile);
	update();
}

void TextView::paintEvent(QPaintEvent *)
{
	QPainter painter(this);
	painter.setRenderHint(QPainter::Antialiasing, true);

	QString txt = text();
	if (txt.isEmpty()) {
		txt = mUI.text;
	}

	if (mPixmap.isNull() && txt.isEmpty())
		return;

	QRect rcBmp = mUI.rectBmp;
	QRect rcTxt = mUI.rectTxt;

	if (mHovered) {		
		rcBmp.adjust(5, 5, 5, 5);
		rcTxt.adjust(5, 5, 5, 5);
	}

	// draw bmp
	painter.drawPixmap(rcBmp, mPixmap); 

	if (txt.size() > 0){
		// draw txt
		QColor txtColor = mUI.txtColor;
		QFont font = this->font();
		font.setPointSize(mUI.fontSize);
		if (mUI.fontWeight > 0) {
            font.setWeight((QFont::Weight)mUI.fontWeight);
//            font.setWeight(QFont::Weight::Thin);
		}	
		painter.setFont(font);	
		
		if (mHovered){
			txtColor = QColor(0, 204, 255);
		}
		painter.setPen(QPen(QBrush(QColor(txtColor)), 1.0));
		painter.setOpacity(1.0);
		if (mbPassWard)
		{
            QString strPW = "******";
			painter.drawText(rcTxt, mUI.flag, strPW);
			return;
		}

		if (mIsTxtChanged) {
			mIsTxtChanged = false;
			mTxtToDraw = txt;
			if (mUI.bCheckTxtLen) {
				mTxtToDraw = modifyDrawText(painter, rcTxt, mUI.flag, mTxtToDraw);
			}
		}
		painter.drawText(rcTxt, mUI.flag, mTxtToDraw);	
	}
	else
	{
		if (mHintText.length() > 0)
		{
			QColor txtColor = QColor(128, 128, 128);
			painter.setPen(QPen(QBrush(QColor(txtColor)), 1.0));
			painter.setOpacity(1.0);
			painter.drawText(rcTxt, mUI.flag, mHintText);	
		}
	}
}

void TextView::setTextEx(const QString& text, int flag)
{
	mUI.text = text;
	mUI.flag = flag;
	mIsTxtChanged = true;
}

QString modifyDrawText(QPainter& painter, const QRect &rcTxt, int flags, QString &text)
{
	QString strResult = text;
	QRect rcBound = painter.boundingRect(rcTxt, flags, text);
	if (rcBound.width() > rcTxt.width() || rcBound.height() > rcTxt.height()){
		//需计算显示字符串
		//qDebug("width = %d-%d, height=%d-%d\n", rcBound.width(), rcTxt.width(), rcBound.height(), rcTxt.height());
		QRect rcTemp = painter.boundingRect(rcTxt,flags, QString("..."));
		QRect rcOneLine = painter.boundingRect(rcTxt, Qt::AlignLeft | Qt::AlignVCenter, text);
		int nNeedTotalLen = rcOneLine.width();
		int nRtLen;
		if (Qt::TextWordWrap & flags)
			nRtLen = rcTxt.height()/rcTemp.height() * (qMin(rcTxt.width(), rcBound.width()));
		else
			nRtLen = 1 * (qMin(rcTxt.width(), rcBound.width()));
		int n = text.size()*(nRtLen-rcTemp.width())/nNeedTotalLen;
		strResult = text.left(n)+QString("...");
		
		//再次验证
		int i=0;
		rcBound = painter.boundingRect(rcTxt, flags, strResult);
		while (rcBound.width() > rcTxt.width() || rcBound.height() > rcTxt.height())
		{
			i++;
			strResult = text.left(n-i)+QString("...");
			rcBound = painter.boundingRect(rcTxt, flags, strResult);
		}
	}
	return strResult;
}
